package sk.umb.nadrz.mc;

import sk.umb.nadrz.model.Axis;
import sk.umb.nadrz.model.BoundingBox;
import sk.umb.nadrz.model.Nadrz;
import sk.umb.nadrz.model.Point;

public class MonteCarlo {

	public Double calculateVolume(double waterLevel, Nadrz nadrz) {
		
		int volume = 0;
		
		//TODO error estimation
		for (int i = 0; i < 100; i++) {
			Point sample = sample(nadrz.getBox());
			if (testSample(nadrz.getOs(), sample, nadrz, waterLevel*100)) {
				volume++;
			}
		}
		Double result = (double) ((volume / 100) * nadrz.getBox().getVolume());
		return result;
	}
	
	private Point sample(BoundingBox box) {
		double x = Math.random() * box.getA();
		double y = Math.random() * box.getB();
		double z = Math.random() * box.getC();
		
		Point point = new Point(x, y, z);
		return point;
	}
	
	private boolean testSample(Axis axis, Point sample, Nadrz nadrz, double hladina) {
		//TODO usecka, konce nadrze
		if ((distance(axis, sample) <= nadrz.getPolomer()) && sample.getZ() <= hladina) {
			return true;
		} else {
			return false;
		}
	}

	private double distance(Axis axis, Point sample) {
		// XXX vzidalenost bodu od priamky v priestore
		return 0;
	}
}
